package cs3343.project.family.UI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import cs3343.project.family.CardController;
import cs3343.project.family.CharacterController;
import cs3343.project.family.ConfigManager;
import cs3343.project.family.Dice;
import cs3343.project.family.Dice2;
import cs3343.project.family.GameController;
import cs3343.project.family.InitGame;
import cs3343.project.family.MapArea;
import cs3343.project.family.ProfileManager;

public class GameFrame extends JFrame {
	
	private GameController gc;

	private JPanel contentPane;
	
	public static JPanel mapPanel = new JPanel();
	static JPanel playerPanel = new JPanel();
	static JPanel debugPanel = new JPanel();
	static JPanel esPanel = new JPanel();
	
	static JLabel curPlay;
	public static JLabel curEsteem;
	static JLabel curIndex;
	static JLabel diceNum;
	static JComboBox<String> drawCard;
	static JButton button;
	
	static JLabel[] imgs = new JLabel[6];
	public static JLabel[] icons = new JLabel[6];
	static JLabel[] names = new JLabel[6];
	public static JLabel[] esteems = new JLabel[6];
		
	public static MapArea[] mapAreas = new MapArea[40];	

	/**
	 * Create the frame.
	 */
	public GameFrame(int w, int h, final GameController gc) {
		
		this.gc = gc;
		
		initUI(w, h);
		
		CardController.importAllCard();

		mapPanel.setBackground(Color.LIGHT_GRAY);
		playerPanel.setBackground(Color.DARK_GRAY);
		esPanel.setBackground(Color.DARK_GRAY);
		debugPanel.setBackground(Color.WHITE);
	    
	    //create dice
	    ImageIcon img_dice = new ImageIcon(ConfigManager.IMAGE_PATH + "dice.jpg");
	    button = new JButton("Roll", img_dice);
	    
	    for (int i = 0; i < 7; i++) {
			mapPanel.add(gc.getDice().getDiceImage(i));
		}		
		gc.getDice().showDiceImage(0);
	    
	    for (int i = 0; i < CharacterController.noOfPlayers; i++) {
		    imgs[i] = new JLabel(CharacterController.players[i].getImage());
		    icons[i] = new JLabel(CharacterController.players[i].getIcon());
		    names[i] = new JLabel(CharacterController.players[i].getName());
		    esteems[i] = new JLabel(String.valueOf(CharacterController.players[i].getEsteem()));
		    playerPanel.add(imgs[i]);
		    
		    if (i == 0)
		    	imgs[i].setVisible(true);
		    else
		    	imgs[i].setVisible(false);
	    }
	    
	    for (int i = CharacterController.noOfPlayers; i < ProfileManager.getProfileSize() - 1; i++)
	    	playerPanel.add(new JLabel(" "));
	    	
	    esPanel.setLayout(new GridLayout(2, 3));
	    playerPanel.setLayout(new GridLayout(7, 1));
    	playerPanel.add(button);
	    
	    
	    for (int i = 0; i < CharacterController.noOfPlayers; i++) {
	    	names[i].setBounds(200, 160 + 20 * i, 200, 200);
	    	esteems[i].setBounds(250, 160 + 20 * i, 200, 200);
	    	icons[i].setBounds(0, 0, 100, 100);
	    	mapPanel.add(names[i]); 
	    	mapPanel.add(esteems[i]); 
	    	mapPanel.add(icons[i]);
	    }
	    
	    //for debug
	    debugPanel.setLayout(new GridLayout(1,5));
	  	curPlay = new JLabel(" ");
	  	curEsteem = new JLabel(" ");
		curIndex = new JLabel(" ");
		diceNum = new JLabel(" ");
		drawCard = new JComboBox<String>();	
		drawCard.addItem("No Card");	
		debugPanel.add(curPlay);
		debugPanel.add(curEsteem);
		debugPanel.add(curIndex);
		debugPanel.add(diceNum);
		debugPanel.add(drawCard);
	    Font f = new Font("Dialog", Font.PLAIN, 24);
	    curPlay.setFont(f);
	    curEsteem.setFont(f);
	    curIndex.setFont(f);
	    diceNum.setFont(f);
	    //drawCard.setFont(f);
	 	  	
		button.addActionListener(new ActionListener() {
			  public void actionPerformed(ActionEvent e) {
				  gc.rollDice();
			  }
		});
		
		mapPanel.setLayout(null);
	    contentPane.add(playerPanel, BorderLayout.EAST);
	    contentPane.add(mapPanel, BorderLayout.CENTER);
	    contentPane.add(debugPanel, BorderLayout.SOUTH);
	}
	
	private void initUI(int w, int h) {
		setTitle("GamePlay");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(300, 20, w, h);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(0, 0, 0, 0));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
	}
	
	public void showPlayerImage(int index) {
		imgs[index].setVisible(true);
	}
	
	public void hidePlayerImage(int index) {
		imgs[index].setVisible(false);
	}
	
	public void setPlayerEsteemLabel(int player) {
		esteems[player].setText(String.valueOf(CharacterController.players[player].getEsteem()));
	}
	
	public void setPlayerIconLocation(int player) {
		int x = mapAreas[CharacterController.players[player].getMapAreaId()].getX();
		int y = mapAreas[CharacterController.players[player].getMapAreaId()].getY();
		icons[player].setBounds(x, y, 100, 100);
		mapPanel.setComponentZOrder(icons[player], 0);
	}
	
	// TODO: Get GC and find the curplayer
	public void setCurrentPlayerEsteemLabel() {
		//curEsteem.setText("SE: " + String.valueOf(CharacterController.players[].getEsteem()));
	}
	
	// TODO: What is the different between se and curSE
	public void setCurrentPlayerEsteemLabel(int player) {
		curEsteem.setText("SE: " + String.valueOf(CharacterController.players[player].getEsteem()));
	}
	
	public void setPlayerNameLabel(int player) {
		curPlay.setText("SE: " + String.valueOf(CharacterController.players[player].getName()));
	}
	
	public void setPlayerLocationLabel(int player) {
		curIndex.setText("SE: " + String.valueOf(CharacterController.players[player].getMapAreaId()));
	}
	
	public void addPlayerAllOwnedCardsInComboBox(int player) {
		for (int i = 0; i < CharacterController.players[player].getNoOfCardOwned(); i++)
			drawCard.addItem(CharacterController.players[player].getCardsOwned().get(i).getDescription());
	}
	
	public void removeAllCardsInComboBox() {
		drawCard.removeAllItems();
	}
}
